package com.dacong.nezha.utils;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class AesUtil {

    public static String getKey() {
        return "b1e454b3b35344229752a6d83545670c";
    }

    public static String getPublicKey() {
        return "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqo8Ju0S/AkDNY2nmVMW6\ndekX/WTrsTsrHI6P/eWAUkK9cSXb0wObE8ffbhGEq1c7Kue55cXN86ygemOgmMRL\n1EO3WThnmglI15PEO8vBbt2fOzjXkMJUqSLEFkhhc4S4BuEr4MOohACqhBPr6WMW\np//uoS4LZs9VIedy5tQ110m8uFzhbD6Vyn8lH1dtLYSfJlDXZnpGA1R7YGk+tC+5\nPQc9qeRhA3TjagyRnhsiBcH7nBcjoD0yfdLC//iZ84va3Bcc8BlKu5ouOmx1LBDI\nRu7XLSOQBHcb8Re1xKxKlU1LIvN2uvr8IYy+XEQA0wStQ9GcZUiM5IqUJ4ve2lSm\nYCm1saM2rJwSaRvwshfupfamXdp9o6z/VcOLne5n7ZyyJ6FT0I5GLO89ezFu48GZ\nrPgnK2R6y1dfwX5Np/X5KlszRa7Gvklgi7OZpn5jIGHN3RPXDMUfueV8Yqwo7Qln\ncfFGxFbjFeF353vTjo7jYS1AG1FSN367DOCsBPTTD1z6OtpuA0ZcYOa1vKgtqfEI\nsvBOzVRGimsgF47GIzOSk4y1zWgBV4ANETPnMMszaEfjPMHKjIIRAiyfcp5h80qe\nuZPsuzJ0FmMKM0K20iXCOY0P927EyXs89PumF1TQCCoy6YqqBpN0DEIJ7ylwJhMK\nIeQ6eIWPCkAnZNJf0ujQa4kCAwEAAQ==\n-----END PUBLIC KEY-----";
    }

    public static String encrypt(String req){
        byte[] keyBytes = DigestUtil.sha256(getKey());
        SecureRandom secureRandom = new SecureRandom();
        byte[] iv = new byte[16];
        secureRandom.nextBytes(iv);
        AES aes = new AES(Mode.CTR, Padding.NoPadding, keyBytes, iv);
        byte[] encrypt = aes.encrypt(req.getBytes());
        byte[] result = new byte[iv.length+encrypt.length];
        // 将第一个数组复制到新数组
        System.arraycopy(iv, 0, result, 0, iv.length);
        // 将第二个数组复制到新数组
        System.arraycopy(encrypt, 0, result, iv.length, encrypt.length);
        return HexUtil.encodeHexStr(result);
    }
    public static String decryptStr(String resp){
        byte[] keyBytes = DigestUtil.sha256(getKey());
        byte[] iv = HexUtil.decodeHex(resp.substring(0,32));
        byte[] body = HexUtil.decodeHex(resp.substring(32));
        AES uaes = new AES(Mode.CTR, Padding.NoPadding, keyBytes, iv);
        return uaes.decryptStr(body);
    }

    public static String rsa(String data){
        // 加载公钥（Base64 编码的 PEM 格式）
        String publicKeyStr = getPublicKey().replace("-----BEGIN PUBLIC KEY-----", "")
                .replace("-----END PUBLIC KEY-----", "")
                .replaceAll("\\s", ""); // 去除 PEM 格式的头部和尾部

        // 解码公钥
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        try{
            PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
            // 使用 RSA/ECB/PKCS1Padding 加密
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
            // 输出 Base64 编码的加密结果
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }catch (Exception e){

        }
        return "";
    }


    public static void main(String[] args) {
        String s = decryptStr("14cff7129276ec3887ba386a1d8face3f1008c86a02849258d660735f7d17d2ca1978fbce969b82efa48a3547772b9416a0070fa86e7662e502b1c2cc956c9ca21fb041911913e4f75e13ad17bb4c969afcc03c210289e1b149dde256196514ba5fbc64dda663d6cf397ffabb9bc74d5b207724fe479648610d327aef74fc13921357cedfc221c84e3cad1b95ae58e079c5cac78a23b341db86208482ef581525a2aeadefc28ad8384593fa51ea22bea113d6dbd5b618b633b2da416ecdc3ccae350d4b85f3794585eefd5ed26ba1563b4c3ea76647e131ad9c49e5269403f5159a553b0c817026f4654471c9a84fe607c");
        System.out.println(s);
    }
}
